package com.paw.ruoyi.admin.controller.sms;

import java.util.List;
import java.util.Map;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.paw.ruoyi.admin.common.annotion.OperatorLog;
import com.paw.ruoyi.admin.common.constant.BusinessType;
import com.paw.ruoyi.admin.common.constant.CommonConstant;
import com.paw.ruoyi.admin.common.domain.Result;
import com.paw.ruoyi.admin.common.domain.PageData;
import com.paw.ruoyi.admin.common.annotion.RequiresPermissions;
import com.paw.ruoyi.admin.common.utils.HuExcelUtil;
import com.paw.ruoyi.admin.common.annotion.SkipLogParam;
import com.paw.ruoyi.admin.common.annotion.SkipResponseLog;
import com.paw.ruoyi.admin.common.domain.PageDomain;
import com.paw.ruoyi.admin.core.util.TableSupport;
import com.paw.ruoyi.admin.domain.SmsSendLog;
import com.paw.ruoyi.admin.service.ISmsSendLogService;


/**
 * 短信发送记录Controller
 *
 * @author admin
 * @date 2022-03-13
 */
@RestController
@RequestMapping("/api/sms/smsLog")
public class SmsSendLogController {

    @Autowired
    private ISmsSendLogService smsSendLogService;

    /**
     * 查询短信发送记录列表
     */
    @RequiresPermissions("sms:smsLog:list")
    @GetMapping("/list")
    public Result<PageData<SmsSendLog>> list(SmsSendLog smsSendLog) {

      PageDomain pageDomain = TableSupport.buildPageRequest();
      LambdaQueryWrapper<SmsSendLog> queryWrapper = new LambdaQueryWrapper<>(smsSendLog);
      // 排序
      Map<String, Object> paramsMap = smsSendLog.getParams();
      if(ObjectUtil.isNotEmpty(paramsMap) &&
          ObjectUtil.isNotEmpty(paramsMap.get(CommonConstant.TABLE_SORT)) &&
          ObjectUtil.isNotEmpty(paramsMap.get(CommonConstant.TABLE_ORDER))){
        String tableSort = (String) paramsMap.get(CommonConstant.TABLE_SORT);
        String tableOrder = (String) paramsMap.get(CommonConstant.TABLE_ORDER);
        if(CommonConstant.CREATE_TIME.equals(tableSort)){
          queryWrapper.orderBy(StrUtil.isNotEmpty(tableSort),CommonConstant.SORT_ASCEND.equals(tableOrder),SmsSendLog::getCreateTime);
        }
      }
      // queryWrapper.orderBy("createTime".equalsIgnoreCase(pageDomain.getOrderBy()),Boolean.parseBoolean(pageDomain.getIsAsc()),SysDictType::getCreateTime);
      Page<SmsSendLog> page = smsSendLogService.page(new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize()), queryWrapper);
      return Result.page(page.getRecords(),page.getTotal());
    }

    /**
     * 导出短信发送记录列表
     */
    @SkipLogParam
    @SkipResponseLog
    @RequiresPermissions("sms:smsLog:export")
    @OperatorLog(title = "短信发送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsSendLog smsSendLog) {
      List<SmsSendLog> list = smsSendLogService.list(Wrappers.lambdaQuery(smsSendLog));
      HuExcelUtil.write(response, list, SmsSendLog.class);
    }

    /**
     * 获取短信发送记录详细信息
     */
    @RequiresPermissions("sms:smsLog:query")
    @GetMapping(value = "/detail/{id}")
    public Result<SmsSendLog> getInfo(@PathVariable("id") Long id) {
      return Result.result(smsSendLogService.getById(id));
    }

    /**
     * 新增短信发送记录
     */
    @RequiresPermissions("sms:smsLog:add")
    @OperatorLog(title = "短信发送记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SmsSendLog smsSendLog) {
      boolean success =  smsSendLogService.save(smsSendLog);
      return Result.result(success);
    }

    /**
     * 修改短信发送记录
     */
    @RequiresPermissions("sms:smsLog:edit")
    @OperatorLog(title = "短信发送记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result<Boolean> edit(@RequestBody SmsSendLog smsSendLog) {
      boolean success =  smsSendLogService.updateById(smsSendLog);
      return Result.result(success);
    }

    /**
     * 删除短信发送记录
     */
    @RequiresPermissions("sms:smsLog:remove")
    @OperatorLog(title = "短信发送记录", businessType = BusinessType.DELETE)
	@PostMapping("/batchRemove/{ids}")
    public Result<Boolean> remove(@PathVariable List<Long> ids) {
      boolean success =  smsSendLogService.removeBatchByIds(ids);
      return Result.result(success);
    }
}
